﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccess;
using DataAccess.Card;
using FOModels.Card;
using System.Data;
using FOModels.Customer;
using System.Net;
namespace Bussiness.Card
{
    public class CardBLL
    {
        private CardDAL dal;
        public CardBLL()
        {
            dal = new CardDAL();
        }

        public List<CardApplyModels> getcontractbycateid(int cardid,int statusid)
        {
            List<CardApplyModels> list = new List<CardApplyModels>();
            CardApplyModels fur;
            DataTable tb = dal.getcontractbycateid(cardid,statusid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new CardApplyModels();
                    fur.CardApplyID = int.Parse(row["CardApplyID"].ToString());
                    fur.CardID = int.Parse(row["CardID"].ToString());
                    fur.CustomerName = row["CustomerName"].ToString();
                    fur.CustomerEmail = row["CustomerEmail"].ToString();
                    fur.CustomerPhone = row["CustomerPhone"].ToString();
                    fur.AppDate = row["AppDate"].ToString();
                    fur.CardName = row["CardName"].ToString();
                    fur.CardImage = row["CardImage"].ToString();
                    fur.StatusName = row["StatusName"].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<CardApplyModels> getcontractall(int cardid)
        {
            List<CardApplyModels> list = new List<CardApplyModels>();
            CardApplyModels fur;
            DataTable tb = dal.getcontractall(cardid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new CardApplyModels();
                    fur.CardApplyID = int.Parse(row["CardApplyID"].ToString());
                    fur.CardID = int.Parse(row["CardID"].ToString());
                    fur.CustomerName = row["CustomerName"].ToString();
                    fur.CustomerEmail = row["CustomerEmail"].ToString();
                    fur.CustomerPhone = row["CustomerPhone"].ToString();
                    fur.AppDate = row["AppDate"].ToString();
                    fur.CardName = row["CardName"].ToString();
                    fur.CardImage = row["CardImage"].ToString();
                    fur.StatusName = row["StatusName"].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public CardApplyModels getcontractbyid(int cardapplyid)
        {

            CardApplyModels fur = new CardApplyModels();
            DataTable tb = dal.getcontractbyid(cardapplyid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new CardApplyModels();
                    fur.CardApplyID = int.Parse(row["CardApplyID"].ToString());
                    fur.CardID = int.Parse(row["CardID"].ToString());
                    fur.CustomerName = row["CustomerName"].ToString();
                    fur.CustomerEmail = row["CustomerEmail"].ToString();
                    fur.CustomerPhone = row["CustomerPhone"].ToString();
                    fur.CustomerIncome = row["CustomerIncome"].ToString();
                    fur.CustomerDOB = row["CustomerDOB"].ToString();
                    fur.AppDate = row["AppDate"].ToString();
                    fur.CardName = row["CardName"].ToString();
                    fur.CardImage = row["CardImage"].ToString();
                    fur.Cost = row["Cost"].ToString();
                    fur.Interest = float.Parse(row["Interest"].ToString());
                    fur.StatusID = int.Parse(row["StatusID"].ToString());
                    fur.StatusName = row["StatusName"].ToString();
                    fur.CompanyImg = row["CompanyLogo"].ToString();
                    fur.CompanyName = row["Name"].ToString();
      
                }
            }
            return fur;
        }
        public List<CardCategoryModels> getCardCategory(int langid)
        {
            List<CardCategoryModels> list = new List<CardCategoryModels>();
            CardCategoryModels fur;
            DataTable tb = dal.getCardCategory(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new CardCategoryModels();
                    fur.CardCategoryID = int.Parse(row["CardCategoryID"].ToString());
                    fur.CardCategoryName = row["CardCategoryName"].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public CardCategoryModels getCardCategoryByID(int cate, int langid)
        {
            CardCategoryModels fur = new CardCategoryModels();
            DataTable tb = dal.getCardCategoryByID(cate, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {

                    fur.CardCategoryID = int.Parse(row["CardCategoryID"].ToString());
                    fur.CardCategoryName = row["CardCategoryID"].ToString();
                }
            }
            return fur;
        }
        public List<CardProperties> getCardPropertiesList(int langid)
        {
            List<CardProperties> list = new List<CardProperties>();
            CardProperties cardpro;
            DataTable tb = dal.getCardProperties(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cardpro = new CardProperties();
                    cardpro.CardPropertiesID = int.Parse(row["CardPropertiesID"].ToString());
                    cardpro.CardPropertiesName = row["CardPropertiesName"].ToString();
                    list.Add(cardpro);
                }
            }
            return list;
        }
        public List<CardProperties> getCardPropertiesListATM(int langid)
        {
            List<CardProperties> list = new List<CardProperties>();
            CardProperties cardpro;
            DataTable tb = dal.getCardPropertiesATM(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cardpro = new CardProperties();
                    cardpro.CardPropertiesID = int.Parse(row["CardPropertiesID"].ToString());
                    cardpro.CardPropertiesName = row["CardPropertiesName"].ToString();
                    list.Add(cardpro);
                }
            }
            return list;
        }
        public List<CreditCard> getCreditCardList(int cateid, int langid)
        {
            List<CreditCard> list = new List<CreditCard>();
            CreditCard cre;
            DataTable tb = dal.getCreditCardList(cateid, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cre = new CreditCard();
                    cre.CardID = int.Parse(row["CardID"].ToString());
                    cre.CardName = row["CardName"].ToString();
                    cre.CompanyID = int.Parse(row["CompanyID"].ToString());
                    cre.BenefitContent = WebUtility.HtmlDecode(row["BenefitContent"].ToString());
                    cre.FurtureContent = WebUtility.HtmlDecode(row["FurtureContent"].ToString());
                    cre.CardImage = row["CardImage"].ToString();
                    cre.CardCategoryID = int.Parse(row["CardCategoryID"].ToString());
                    cre.CardPropertiesID = int.Parse(row["CardPropertiesID"].ToString());
                    cre.Description = WebUtility.HtmlDecode(row["Description"].ToString());
                    cre.Cost = double.Parse(row["Cost"].ToString());
                    cre.Interest = float.Parse(row["Interest"].ToString());
                    cre.MoreBenefit = WebUtility.HtmlDecode(row["MoreBenefit"].ToString());
                    cre.MoreFee = WebUtility.HtmlDecode(row["MoreFee"].ToString());
                    cre.MoreInfo = WebUtility.HtmlDecode(row["MoreInfo"].ToString());
                    cre.Display = bool.Parse(row["Display"].ToString());
                    cre.CompanyImg = row["CompanyLogo"].ToString();
                    cre.CompanyName = row["Name"].ToString();

                    list.Add(cre);
                }
            }
            return list;
        }
        public List<CreditCard> getcardtomanager(int CardCate, int langid)
        {
            List<CreditCard> list = new List<CreditCard>();
            CreditCard cre;
            DataTable tb = dal.getallcard(CardCate, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cre = new CreditCard();
                    cre.CardID = int.Parse(row["CardID"].ToString());
                    cre.CardName = row["CardName"].ToString();
                    cre.CompanyID = int.Parse(row["CompanyID"].ToString());
                    cre.BenefitContent = WebUtility.HtmlDecode(row["BenefitContent"].ToString());
                    cre.FurtureContent = WebUtility.HtmlDecode(row["FurtureContent"].ToString());
                    cre.CardImage = row["CardImage"].ToString();
                    cre.CardCategoryID = int.Parse(row["CardCategoryID"].ToString());
                    cre.CardPropertiesID = int.Parse(row["CardPropertiesID"].ToString());
                    cre.Description = WebUtility.HtmlDecode(row["Description"].ToString());
                    cre.Cost = double.Parse(row["Cost"].ToString());
                    cre.Interest = float.Parse(row["Interest"].ToString());
                    cre.MoreBenefit = WebUtility.HtmlDecode(row["MoreBenefit"].ToString());
                    cre.MoreFee = WebUtility.HtmlDecode(row["MoreFee"].ToString());
                    cre.MoreInfo = WebUtility.HtmlDecode(row["MoreInfo"].ToString());
                    cre.Display = bool.Parse(row["Display"].ToString());
                    cre.CompanyImg = row["CompanyLogo"].ToString();
                    cre.CompanyName = row["Name"].ToString();
                    cre.FlagImage = row["FlagImage"].ToString();
                    list.Add(cre);
                }
            }
            return list;
        }
        public List<CreditCard> getCreditCardByProvider(int CardCategoryID, int CompanyID, int langid)
        {
            List<CreditCard> list = new List<CreditCard>();
            CreditCard cre;
            DataTable tb = dal.getCreditCardByProvider(CardCategoryID, CompanyID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cre = new CreditCard();
                    cre.CardID = int.Parse(row["CardID"].ToString());
                    cre.CardName = row["CardName"].ToString();
                    cre.CompanyID = int.Parse(row["CompanyID"].ToString());
                    cre.BenefitContent = WebUtility.HtmlDecode(row["BenefitContent"].ToString());
                    cre.FurtureContent = WebUtility.HtmlDecode(row["FurtureContent"].ToString());
                    cre.CardImage = row["CardImage"].ToString();
                    cre.CardCategoryID = int.Parse(row["CardCategoryID"].ToString());
                    cre.CardPropertiesID = int.Parse(row["CardPropertiesID"].ToString());
                    cre.Description = WebUtility.HtmlDecode(row["Description"].ToString());
                    cre.Cost = double.Parse(row["Cost"].ToString());
                    cre.Interest = float.Parse(row["Interest"].ToString());
                    cre.MoreBenefit = WebUtility.HtmlDecode(row["MoreBenefit"].ToString());
                    cre.MoreFee = WebUtility.HtmlDecode(row["MoreFee"].ToString());
                    cre.MoreInfo = WebUtility.HtmlDecode(row["MoreInfo"].ToString());
                    cre.Display = bool.Parse(row["Display"].ToString());
                    cre.CompanyImg = row["CompanyLogo"].ToString();
                    cre.CompanyName = row["Name"].ToString();

                    list.Add(cre);
                }
            }
            return list;
        }
        public List<CreditCard> getcreditcardrelati(int CardCategoryID, int CompanyID, int CardID, int langid)
        {
            List<CreditCard> list = new List<CreditCard>();
            CreditCard cre;
            DataTable tb = dal.getcreditcardrelati(CardCategoryID, CompanyID, CardID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cre = new CreditCard();
                    cre.CardID = int.Parse(row["CardID"].ToString());
                    cre.CardName = row["CardName"].ToString();
                    cre.CompanyID = int.Parse(row["CompanyID"].ToString());
                    cre.BenefitContent = WebUtility.HtmlDecode(row["BenefitContent"].ToString());
                    cre.FurtureContent = WebUtility.HtmlDecode(row["FurtureContent"].ToString());
                    cre.CardImage = row["CardImage"].ToString();
                    cre.CardCategoryID = int.Parse(row["CardCategoryID"].ToString());
                    cre.CardPropertiesID = int.Parse(row["CardPropertiesID"].ToString());
                    cre.Description = WebUtility.HtmlDecode(row["Description"].ToString());
                    cre.Cost = double.Parse(row["Cost"].ToString());
                    cre.Interest = float.Parse(row["Interest"].ToString());
                    cre.MoreBenefit = WebUtility.HtmlDecode(row["MoreBenefit"].ToString());
                    cre.MoreFee = WebUtility.HtmlDecode(row["MoreFee"].ToString());
                    cre.MoreInfo = WebUtility.HtmlDecode(row["MoreInfo"].ToString());
                    cre.Display = bool.Parse(row["Display"].ToString());
                    cre.CompanyImg = row["CompanyLogo"].ToString();
                    cre.CompanyName = row["Name"].ToString();


                    list.Add(cre);
                }
            }
            return list;
        }
        public CreditCard getCreditCardByID(int CardID, int langid)
        {
            CreditCard cre = new CreditCard();
            DataTable tb = dal.getCreditCardByID(CardID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cre = new CreditCard();
                    cre.CardID = int.Parse(row["CardID"].ToString());
                    cre.CardName = row["CardName"].ToString();
                    cre.CompanyID = int.Parse(row["CompanyID"].ToString());
                    cre.BenefitContent = WebUtility.HtmlDecode(row["BenefitContent"].ToString());
                    cre.FurtureContent = WebUtility.HtmlDecode(row["FurtureContent"].ToString());
                    cre.CardImage = row["CardImage"].ToString();
                    cre.CardCategoryID = int.Parse(row["CardCategoryID"].ToString());
                    cre.CardPropertiesID = int.Parse(row["CardPropertiesID"].ToString());
                    cre.Description = WebUtility.HtmlDecode(row["Description"].ToString());
                    cre.Cost = double.Parse(row["Cost"].ToString());
                    cre.Interest = float.Parse(row["Interest"].ToString());
                    cre.MoreBenefit = WebUtility.HtmlDecode(row["MoreBenefit"].ToString());
                    cre.MoreFee = WebUtility.HtmlDecode(row["MoreFee"].ToString());
                    cre.MoreInfo = WebUtility.HtmlDecode(row["MoreInfo"].ToString());
                    cre.Display = bool.Parse(row["Display"].ToString());
                    cre.CompanyImg = row["CompanyLogo"].ToString();
                    cre.CompanyName = row["Name"].ToString();
                }
                return cre;
            }
            else
                return null;
        }
        public CreditCard getCreditCardByIDManager(int CardID, int langid)
        {
            CreditCard cre = new CreditCard();
            DataTable tb = dal.getCreditCardByIDManager(CardID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cre = new CreditCard();
                    cre.CardID = int.Parse(row["CardID"].ToString());
                    cre.CardName = row["CardName"].ToString();
                    cre.CompanyID = int.Parse(row["CompanyID"].ToString());
                    cre.BenefitContent = WebUtility.HtmlDecode(row["BenefitContent"].ToString());
                    cre.FurtureContent = WebUtility.HtmlDecode(row["FurtureContent"].ToString());
                    cre.CardImage = row["CardImage"].ToString();
                    cre.CardCategoryID = int.Parse(row["CardCategoryID"].ToString());
                    cre.CardPropertiesID = int.Parse(row["CardPropertiesID"].ToString());
                    cre.Description = WebUtility.HtmlDecode(row["Description"].ToString());
                    cre.Cost = double.Parse(row["Cost"].ToString());
                    cre.Interest = float.Parse(row["Interest"].ToString());
                    cre.MoreBenefit = WebUtility.HtmlDecode(row["MoreBenefit"].ToString());
                    cre.MoreFee = WebUtility.HtmlDecode(row["MoreFee"].ToString());
                    cre.MoreInfo = WebUtility.HtmlDecode(row["MoreInfo"].ToString());
                    cre.Display = bool.Parse(row["Display"].ToString());
                    cre.CompanyImg = row["CompanyLogo"].ToString();
                    cre.CompanyName = row["Name"].ToString();
                }
                return cre;
            }
            else
                return null;
        }
        public CreditCard getTop1Card()
        {
            CreditCard cre = new CreditCard();
            DataTable tb = dal.getTop1Card();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cre = new CreditCard();
                    cre.CardID = int.Parse(row[0].ToString());
                    cre.CardName = row[1].ToString();
                }
                return cre;
            }
            else
                return null;
        }
        public List<CreditCard> getCreditCardListByProperties(int cateid, int CardPropertiesID, int langid)
        {
            List<CreditCard> list = new List<CreditCard>();
            CreditCard cre;
            DataTable tb = dal.getCreditCardListByProperties(cateid, CardPropertiesID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cre = new CreditCard();
                    cre.CardID = int.Parse(row["CardID"].ToString());
                    cre.CardName = row["CardName"].ToString();
                    cre.CompanyID = int.Parse(row["CompanyID"].ToString());
                    cre.BenefitContent = WebUtility.HtmlDecode(row["BenefitContent"].ToString());
                    cre.FurtureContent = WebUtility.HtmlDecode(row["FurtureContent"].ToString());
                    cre.CardImage = row["CardImage"].ToString();
                    cre.CardCategoryID = int.Parse(row["CardCategoryID"].ToString());
                    cre.CardPropertiesID = int.Parse(row["CardPropertiesID"].ToString());
                    cre.Description = WebUtility.HtmlDecode(row["Description"].ToString());
                    cre.Cost = double.Parse(row["Cost"].ToString());
                    cre.Interest = float.Parse(row["Interest"].ToString());
                    cre.MoreBenefit = WebUtility.HtmlDecode(row["MoreBenefit"].ToString());
                    cre.MoreFee = WebUtility.HtmlDecode(row["MoreFee"].ToString());
                    cre.MoreInfo = WebUtility.HtmlDecode(row["MoreInfo"].ToString());
                    cre.Display = bool.Parse(row["Display"].ToString());
                    cre.CompanyImg = row["CompanyLogo"].ToString();
                    cre.CompanyName = row["Name"].ToString();
                    list.Add(cre);
                }
            }
            return list;
        }
        public List<CreditCard> getcardbycom_pro(int CardCategoryID, int CardPropertiesID, int CompanyID, int langid)
        {
            List<CreditCard> list = new List<CreditCard>();
            CreditCard cre;
            DataTable tb = dal.getcardbycom_pro(CardCategoryID, CardPropertiesID, CompanyID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cre = new CreditCard();
                    cre.CardID = int.Parse(row["CardID"].ToString());
                    cre.CardName = row["CardName"].ToString();
                    cre.CompanyID = int.Parse(row["CompanyID"].ToString());
                    cre.BenefitContent = WebUtility.HtmlDecode(row["BenefitContent"].ToString());
                    cre.FurtureContent = WebUtility.HtmlDecode(row["FurtureContent"].ToString());
                    cre.CardImage = row["CardImage"].ToString();
                    cre.CardCategoryID = int.Parse(row["CardCategoryID"].ToString());
                    cre.CardPropertiesID = int.Parse(row["CardPropertiesID"].ToString());
                    cre.Description = WebUtility.HtmlDecode(row["Description"].ToString());
                    cre.Cost = double.Parse(row["Cost"].ToString());
                    cre.Interest = float.Parse(row["Interest"].ToString());
                    cre.MoreBenefit = WebUtility.HtmlDecode(row["MoreBenefit"].ToString());
                    cre.MoreFee = WebUtility.HtmlDecode(row["MoreFee"].ToString());
                    cre.MoreInfo = WebUtility.HtmlDecode(row["MoreInfo"].ToString());
                    cre.Display = bool.Parse(row["Display"].ToString());
                    cre.CompanyImg = row["CompanyLogo"].ToString();
                    cre.CompanyName = row["Name"].ToString();
                    list.Add(cre);
                }
            }
            return list;
        }
        public int InsertCardApplyInfo(int CustomerID, int CardID)
        {
            if (dal.InsertCardApplyInfo(CustomerID, CardID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public CardApplyInfoModels SearchCardApplyInfo(int CustomerID, int CardID)
        {
            CardApplyInfoModels cus = new CardApplyInfoModels();
            DataTable tb = dal.SearchCardApplyInfo(CustomerID, CardID);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cus.CustomerID = int.Parse(row[0].ToString());
                    cus.CardID = int.Parse(row[1].ToString());
                }
                return cus;
            }
            else
            {
                return null;
            }

        }
        public int InsertCard(CreditCard card)
        {
            if (dal.InsertCard(card) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertCardLang(CreditCard card, int langid)
        {
            if (dal.InsertCardLang(card, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int UpdateCard(CreditCard card, int langid)
        {
            if (dal.UpdateCard(card) > 0 && dal.UpdateCardLang(card, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int UpdateStatus(CardApplyModels card)
        {
            if (dal.UpdateStatus(card) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteCard(int CardID)
        {
            if (dal.DeleteCardLang(CardID) > 0 && dal.DeleteCard(CardID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteCardApply(int CardApplyID)
        {
            if (dal.DeleteCardApply(CardApplyID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public List<CreditCard> getallcard(int langid)
        {
            List<CreditCard> list = new List<CreditCard>();
            CreditCard cre;
            DataTable tb = dal.getAllCard(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    cre = new CreditCard();
              
                   
                    cre.CardID = int.Parse(row["CardID"].ToString());
                    cre.CardName = row["CardName"].ToString();
                    cre.CompanyID = int.Parse(row["CompanyID"].ToString());
                    cre.BenefitContent = WebUtility.HtmlDecode(row["BenefitContent"].ToString());
                    cre.FurtureContent = WebUtility.HtmlDecode(row["FurtureContent"].ToString());
                    cre.CardImage = row["CardImage"].ToString();
                    cre.CardCategoryID = int.Parse(row["CardCategoryID"].ToString());
                    cre.CardPropertiesID = int.Parse(row["CardPropertiesID"].ToString());
                    cre.Description = WebUtility.HtmlDecode(row["Description"].ToString());
                    cre.Cost = double.Parse(row["Cost"].ToString());
                    cre.Interest = float.Parse(row["Interest"].ToString());
                    cre.MoreBenefit = WebUtility.HtmlDecode(row["MoreBenefit"].ToString());
                    cre.MoreFee = WebUtility.HtmlDecode(row["MoreFee"].ToString());
                    cre.MoreInfo = WebUtility.HtmlDecode(row["MoreInfo"].ToString());
                    cre.Display = bool.Parse(row["Display"].ToString());
                    cre.CompanyImg = row["CompanyLogo"].ToString();
                    cre.CompanyName = row["CompanyName"].ToString();
                    cre.FlagImage = row["FlagImage"].ToString();
                    list.Add(cre);
                }
            }
            return list;
        }
        public int CardApply(int CardID, CardApplyModels card)
        {
            if (dal.CardApply(CardID, card) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }
}
